package com.itheima.controller;

import com.itheima.pojo.Class;
import com.itheima.pojo.User;
import com.itheima.service.LoginService;
import com.itheima.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/v1/login")
public class LoginController {
    @Autowired
    private LoginService loginService;
    @Autowired
    private HttpServletRequest httpServletRequest;
    //判断是否是老师
    @GetMapping("/{mobile}")
    public ResponseResult Getroleid(@PathVariable String mobile){
        User user = loginService.SelectByMobile(mobile);
        if(user==null){
            return new ResponseResult(1,"该账号不存在",null);
        }
        if(user.getRole_id()==1){
            ResponseResult rr = new ResponseResult();
            List<Class> classes = loginService.SelectClass();
            rr.setCode(0);
            rr.setErrorMessage("老师账号");
            rr.setData(classes);
            return rr;
        }else {
            return new ResponseResult(0,"学生账号",null);
        }
    }
    //判断密码和账号是否正确
    @PostMapping("/in")
    public ResponseResult Login(@RequestBody User user ){
        User user1 = loginService.SelectByMobileAndName(user);
        if(user1!=null){
            //session必须放在Controller层
            HttpSession session = httpServletRequest.getSession();
            session.setAttribute("user",user);
            return new ResponseResult(0,"登录成功");
        }
        return new ResponseResult(1,"登录失败");
    }

}
